﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using DataTransferObject;
using DevComponents.DotNetBar.Controls;

namespace PKSoftManagement
{
    public partial class FormDanhMuc : UserControl
    {
        dynamic BLL;
        public FormDanhMuc(dynamic BLLType)
        {
            InitializeComponent();
            BLL = BLLType;
            loadGridBox();
            switchEnable(true, "");
            //BindingSource bs = new BindingSource();
            //bs.DataSource = BLL.GetAll();
            DGView.DataSource = BLL.GetAll();
        }

        private void bt_Click(object sender, EventArgs e)
        {
            if (((Button)sender) == them)
            {
                ClearValue();
                switchEnable(false, "Thêm");
            }
            else if (((Button)sender) == sua)
            {
                if (DGView.CurrentRow != null)
                {
                    switchEnable(false, "Sửa");
                }
            }
            else
            {
                if (DGView.CurrentRow != null)
                {
                    switchEnable(true, "");
                    if (DGView.CurrentRow != null)
                    {
                        DialogResult result = MessageBox.Show("Bạn có muốn xóa dữ liệu này:", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (result == DialogResult.Yes)
                        {
                            BLL.Delete(int.Parse(DGView.CurrentRow.Cells[0].Value.ToString()));
                            var list = BLL.GetAll();
                            DGView.DataSource = list;
                        }
                        else
                        {
                            ReleaseValue();
                        }
                    }
                }
            }
        }

        private void DGView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            switchEnable(true, "");
            ReleaseValue();
        }

        private void AcceptBT_Click(object sender, EventArgs e)
        {
            foreach (var item in BLL.create.GetType().GetProperties())
            {
                string tmp = item.Name;
                Control find = Controls.Find(tmp, true).FirstOrDefault();
                if (find is TextBox)
                {
                    item.SetValue(BLL.create, find.Text, null);
                }
                else if (find is CheckBox)
                {
                    item.SetValue(BLL.create, ((CheckBox)find).Checked, null);
                }
                else if (find is NumericUpDown)
                {
                    item.SetValue(BLL.create, ((int)((NumericUpDown)find).Value), null);
                }
                else if (find is DateTimePicker)
                {
                    item.SetValue(BLL.create, ((System.DateTime)((DateTimePicker)find).Value), null);
                }
            }
            if (((Button)sender).Text == "Thêm")
            {
                if (MessageBox.Show("Bạn có muốn thêm dữ liệu này:", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    BLL.Insert(BLL.create, BLL.create.Id);
                    DGView.DataSource = BLL.GetAll();
                }
                switchEnable(true, "");
                ReleaseValue();
            }
            else
            {
                if (MessageBox.Show("Bạn có muốn sửa dữ liệu này", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    BLL.Update(BLL.create, BLL.create.Id);
                    DGView.DataSource = BLL.GetAll();
                }
                switchEnable(true, "");
                ReleaseValue();
            }
        }
        private void huy_Click(object sender, EventArgs e)
        {
            switchEnable(true, "");
            ReleaseValue();
        }

        private void ReleaseValue()
        {
            if (DGView.CurrentRow != null)
            {
                foreach (DataGridViewCell item in DGView.CurrentRow.Cells)
                {
                    Control find = Controls.Find(DGView.Columns[item.ColumnIndex].Name, true).FirstOrDefault();
                    if (item.Value != null)
                    {
                        if (find is TextBox)
                        {
                            find.Text = item.Value.ToString();
                        }
                        else if (find is NumericUpDown)
                        {
                            find.Text = item.Value.ToString();
                        }
                        else if (find is CheckBox)
                        {
                            find.Text = item.Value.ToString();
                            ((CheckBox)find).Checked = (bool)item.Value;
                        }
                        else if (find is DateTimePicker)
                        {
                            ((DateTimePicker)find).Value = (System.DateTime)item.Value;
                        }

                    }
                }
            }
        }
        private void switchEnable(bool bl, string ten)
        {
            them.Enabled = bl;
            sua.Enabled = bl;
            AcceptBT.Enabled = !bl;
            AcceptBT.Text = ten;
            huy.Enabled = !bl;
            foreach (var item in BLL.create.GetType().GetProperties())
            {
                string tmp = item.Name;
                dynamic find = Controls.Find(tmp, true).FirstOrDefault();
                if (find is TextBox)
                {
                    find.ReadOnly = bl;
                }
                else if (find is CheckBox)
                {
                    find.Enabled = !bl;
                }
                else if (find is NumericUpDown)
                {
                    find.ReadOnly = bl;
                }
                else if (find is DateTimePicker)
                {
                    find.Enabled = !bl;
                }
            }
        }
        private void loadGridBox()
        {
            int top = 0;
            foreach (var item in BLL.create.GetType().GetProperties())
            {
                Label lb = new Label() { Text = item.Name, AutoSize = true, Location = new Point(25, top) };
                top += 25;
                if (item.PropertyType == typeof(string))
                {
                    TextBox tb = new TextBox() { Name = item.Name, Width = 200, Location = new Point(25, top) };
                    grpn.Controls.AddRange(new Control[] { lb, tb });
                }
                else if (item.PropertyType == typeof(int) || item.PropertyType == typeof(Nullable<int>))
                {
                    NumericUpDown nud = new NumericUpDown() { Name = item.Name, Width = 200, Location = new Point(25, top) };
                    grpn.Controls.AddRange(new Control[] { lb, nud });
                }
                else if (item.PropertyType == typeof(bool) || item.PropertyType == typeof(Nullable<bool>))
                {
                    CheckBox ch = new CheckBox() { Name = item.Name, Location = new Point(25, top), AutoSize = true };
                    grpn.Controls.AddRange(new Control[] { lb, ch });
                }
                else if (item.PropertyType == typeof(System.DateTime) || item.PropertyType == typeof(Nullable<System.DateTime>))
                {
                    DateTimePicker pic = new DateTimePicker() { Name = item.Name, Location = new Point(25, top) };
                    grpn.Controls.AddRange(new Control[] { lb, pic });
                }
                top += 40;
            }
        }
        private void ClearValue()
        {
            foreach (dynamic c in grpn.Controls)
            {
                if (c is CheckBox)
                {
                    c.Checked = false;
                }
                else if (c is NumericUpDown)
                {
                    c.Value = 0;
                }
                else if (c is DateTimePicker)
                {
                    c.Value = System.DateTime.Now;
                }
                if (!(c is Label))
                    c.Text = "";
            }
        }
    }
}
